package com.sskj.lib.mvchelper;

import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.MutableLiveData;

import com.shizhefei.mvc.IAsyncDataSource;
import com.shizhefei.mvc.RequestHandle;
import com.shizhefei.mvc.ResponseSender;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-07-27 08:27
 * 下拉刷新，展示各种状态界面，请求数据
 */
public class PageDataSource<T> implements IAsyncDataSource<List<T>> {
    private MutableLiveData tMutableLiveData;
    private ResponseSender sender;
    private OnLoadSource onLoadSource;
    private CoolRefreshView coolRefreshView;
    private int pageSize;
    private int page = 1;
    private boolean hasMore = true;

    public interface OnLoadSource<T> {
        Flowable<List<T>> loadData(int page);
    }

    public PageDataSource(LifecycleOwner lifecycleOwner, CoolRefreshView coolRefreshView, OnLoadSource onLoadSource) {
        this(lifecycleOwner, coolRefreshView, onLoadSource, 10);
    }

    public PageDataSource(LifecycleOwner lifecycleOwner, CoolRefreshView coolRefreshView, OnLoadSource onLoadSource, int pageSize) {
        this.pageSize = pageSize;
        this.coolRefreshView = coolRefreshView;
        this.onLoadSource = onLoadSource;
        tMutableLiveData = new MutableLiveData<>();
        tMutableLiveData.observe(lifecycleOwner, t -> {
            if (sender != null) {
                sender.sendData(t);
            }
        });
    }

    @Override
    public RequestHandle refresh(ResponseSender<List<T>> sender) throws Exception {
        page = 1;
        return loadData(sender);

    }

    @Override
    public RequestHandle loadMore(ResponseSender<List<T>> sender) throws Exception {
        page++;
        return loadData(sender);
    }

    private RequestHandle loadData(ResponseSender<List<T>> sender) {
        this.sender = sender;
        DisposableSubscriber<List<T>> subscriber = new DisposableSubscriber<List<T>>() {
            @Override
            public void onNext(List<T> data) {
                if (data.size() >= pageSize) {
                    hasMore = true;
                } else {
                    hasMore = false;
                }
                tMutableLiveData.postValue(data);
            }

            @Override
            public void onError(Throwable e) {
                sender.sendError(new Exception(e));
                if (coolRefreshView != null) {
                    if (coolRefreshView.isRefreshing()) {
                        coolRefreshView.setRefreshing(false);

                    }
                }
            }

            @Override
            public void onComplete() {
                if (coolRefreshView != null) {
                    if (coolRefreshView.isRefreshing()) {
                        coolRefreshView.setRefreshing(false);

                    }
                }
            }
        };
        onLoadSource.loadData(page).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
        return new RequestHandle() {
            @Override
            public void cancle() {
                if (!subscriber.isDisposed()) {
                    subscriber.dispose();
                }
            }

            @Override
            public boolean isRunning() {
                return !subscriber.isDisposed();
            }
        };
    }

    @Override
    public boolean hasMore() {
        return hasMore;
    }

}
